#include <Arduino.h>

#if defined(CH551) || defined(CH552)
void digitalWriteHighDirectLut(uint8_t pin) {
  //'dpl' (LSB),'dph','b' & 'acc'
  pin;
  // clang-format off
  __asm__(
    ";setb and return are 2 bytes, mul by 3       \n"
    "    mov a,dpl                                \n"
    "    add a,dpl                                \n"
    "    add a,dpl                                \n"
    "    mov dptr,#direct_write_high_lut$         \n"
    "    jmp @a+dptr                              \n"

    "direct_write_high_lut$:                      \n"
    ";No PORT0                                    \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    
    ";PORT1                                       \n"
    "    setb _P1_0                               \n"
    "    ret                                      \n"
    "    setb _P1_1                               \n"
    "    ret                                      \n"
    "    setb _P1_2                               \n"
    "    ret                                      \n"
    "    setb _P1_3                               \n"
    "    ret                                      \n"
    "    setb _P1_4                               \n"
    "    ret                                      \n"
    "    setb _P1_5                               \n"
    "    ret                                      \n"
    "    setb _P1_6                               \n"
    "    ret                                      \n"
    "    setb _P1_7                               \n"
    "    ret                                      \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    
    ";No PORT2                                    \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"

    ";PORT3                                       \n"
    "    setb _P3_0                               \n"
    "    ret                                      \n"
    "    setb _P3_1                               \n"
    "    ret                                      \n"
    "    setb _P3_2                               \n"
    "    ret                                      \n"
    "    setb _P3_3                               \n"
    "    ret                                      \n"
    "    setb _P3_4                               \n"
    "    ret                                      \n"
    "    setb _P3_5                               \n"
    "    ret                                      \n"
    "    setb _P3_6                               \n"
    "    ret                                      \n"
    "    setb _P3_7                               \n"
    "    ret                                      \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"    
  );
  // clang-format on
}

void digitalWriteLowDirectLut(uint8_t pin) {
  //'dpl' (LSB),'dph','b' & 'acc'
  pin;
  // clang-format off
  __asm__(
    ";clr and return are 2 bytes, mul by 3        \n"
    "    mov a,dpl                                \n"
    "    add a,dpl                                \n"
    "    add a,dpl                                \n"
    "    mov dptr,#direct_write_low_lut$          \n"
    "    jmp @a+dptr                              \n"

    "direct_write_low_lut$:                        \n"
    ";No PORT0                                    \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    
    ";PORT1                                       \n"
    "    clr _P1_0                                \n"
    "    ret                                      \n"
    "    clr _P1_1                                \n"
    "    ret                                      \n"
    "    clr _P1_2                                \n"
    "    ret                                      \n"
    "    clr _P1_3                                \n"
    "    ret                                      \n"
    "    clr _P1_4                                \n"
    "    ret                                      \n"
    "    clr _P1_5                                \n"
    "    ret                                      \n"
    "    clr _P1_6                                \n"
    "    ret                                      \n"
    "    clr _P1_7                                \n"
    "    ret                                      \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    
    ";No PORT2                                    \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"

    ";PORT3                                       \n"
    "    clr _P3_0                                \n"
    "    ret                                      \n"
    "    clr _P3_1                                \n"
    "    ret                                      \n"
    "    clr _P3_2                                \n"
    "    ret                                      \n"
    "    clr _P3_3                                \n"
    "    ret                                      \n"
    "    clr _P3_4                                \n"
    "    ret                                      \n"
    "    clr _P3_5                                \n"
    "    ret                                      \n"
    "    clr _P3_6                                \n"
    "    ret                                      \n"
    "    clr _P3_7                                \n"
    "    ret                                      \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"    
  );
  // clang-format on
}
#endif